package org.corey.share.db.service;

import org.corey.share.auth.PermissionUtil;
import org.corey.share.db.entity.User;
import org.corey.share.misc.Constants;
import org.corey.share.throwable.ValidationUserException;

import com.opensymphony.xwork2.ActionContext;

public class AuthServiceImpl implements AuthService {

	public static UserService userService = new UserServiceImpl();
	private static AuthServiceImpl instance = new AuthServiceImpl();

	public static AuthService getInstance() {
		return instance;
	}

	private AuthServiceImpl() {

	}

	public Long getUserId() {
		return (Long) (getUser().getId());
	}

	public User getUser() {
		return (User) (ActionContext.getContext().getSession()
				.get(Constants.USER_KEY));
	}

	public boolean isLogin() {
		return ActionContext.getContext().getSession().get(Constants.USER_KEY) != null;
	}

	public void layout() {
		ActionContext.getContext().getSession().put(Constants.USER_KEY, null);
	}

	public boolean login(String userName, String password, String checkCode)
			throws ValidationUserException {
		User user = userService.validUser(userName, password);
		ActionContext.getContext().getSession().put(Constants.USER_KEY, user);
		return true;
	}

	@Override
	public boolean isRequireAdminPermission(String requestPath) {
		return PermissionUtil.getAdminPermission().allow(requestPath);
	}
}
